/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package AccesoWS;

import DTOs.DTOHotel;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import webservices.ReservationVo;
import webservices.RoomVo;

/**
 *
 * @author Julian
 */
public class WSHotel {
    
    public static List<webservices.RoomVo> consultarHabitaciones() throws Exception{
        try { // Call Web Service Operation
        webservices.WebServiceHabitacionService service = new webservices.WebServiceHabitacionService();
        webservices.WebServiceHabitacion port = service.getWebServiceHabitacionPort();
        // TODO process result here
        java.util.List<webservices.RoomVo> result = port.consultarHabitaciones();
        System.out.println("Result = "+result);
        return result;
        } catch (Exception ex) {
            throw ex;
            // TODO handle custom exceptions here
        }
    }

    public static List<webservices.RoomVo> consultarHabitacionesDisponibles() throws Exception{

        try { // Call Web Service Operation
            webservices.WebServiceHabitacionService service = new webservices.WebServiceHabitacionService();
            webservices.WebServiceHabitacion port = service.getWebServiceHabitacionPort();
            // TODO process result here
            java.util.List<webservices.RoomVo> result = port.getRoomsAvailable();
            System.out.println("Result = "+result);
            return result;
        } catch (Exception ex) {
            throw ex;
            // TODO handle custom exceptions here
        }
    }

    public static ArrayList<DTOs.DTOHotel> getAvailableRooms() throws Exception{
        return toDTO(consultarHabitacionesDisponibles());
    }

    public static String getTypeName(int typeId) throws Exception{

        try { // Call Web Service Operation
            webservices.WebServiceHabitacionService service = new webservices.WebServiceHabitacionService();
            webservices.WebServiceHabitacion port = service.getWebServiceHabitacionPort();
            // TODO initialize WS operation arguments here
            // TODO process result here
            java.lang.String result = port.roomType(typeId);
            System.out.println("Result = "+result);
            return result;
        } catch (Exception ex) {
            throw ex;
            // TODO handle custom exceptions here
        }
    }

    public static double getRoomCost(int typeId) throws Exception{

        try { // Call Web Service Operation
            webservices.WebServiceHabitacionService service = new webservices.WebServiceHabitacionService();
            webservices.WebServiceHabitacion port = service.getWebServiceHabitacionPort();
            // TODO initialize WS operation arguments here
            // TODO process result here
            double result = port.costRoom(typeId);
            System.out.println("Result = "+result);
            return result;
        } catch (Exception ex) {
            throw ex;
            // TODO handle custom exceptions here
        }

    }

    public static int reservarHabitacion(int day1, int month1, int year1, int day2, int month2, int year2, int idHabitacion, String identificacion) throws Exception{

        try { // Call Web Service Operation
            webservices.WebServiceReservaService service = new webservices.WebServiceReservaService();
            webservices.WebServiceReserva port = service.getWebServiceReservaPort();
            // TODO initialize WS operation arguments here
            webservices.ReservationVo reservavo = new webservices.ReservationVo();
            DatatypeFactory factory = DatatypeFactory.newInstance();

            XMLGregorianCalendar fecha1 = factory.newXMLGregorianCalendar();
            fecha1.setDay(day1);
            fecha1.setMonth(month1);
            fecha1.setYear(year1);
            fecha1.setHour(0);
            fecha1.setMinute(0);
            fecha1.setSecond(0);

            XMLGregorianCalendar fecha2 = factory.newXMLGregorianCalendar();
            fecha2.setDay(day2);
            fecha2.setMonth(month2);
            fecha2.setYear(year2);
            fecha2.setHour(0);
            fecha2.setMinute(0);
            fecha2.setSecond(0);

        reservavo.setIdRoom(getRoomVo(idHabitacion));
        reservavo.setUserIdentification(consultarUsuario(identificacion));
        reservavo.setReservationDate(fecha1);
        reservavo.setExpirationDate(fecha2);

            // TODO process result here
            int result = port.reservarHabitacion(reservavo);
            System.out.println("Result = "+result);
            return result;
        } catch (Exception ex) {
            throw ex;
            // TODO handle custom exceptions here
        }
    }

    public static webservices.RoomVo getRoomVo(int idRoom) throws Exception{
        try {
            List<webservices.RoomVo> habs = consultarHabitacionesDisponibles();
            webservices.RoomVo rv = new webservices.RoomVo();
            for (int i = 0; i < habs.size(); i++) {
                if (habs.get(i).getId() == idRoom) {
                    rv = habs.get(i);
                }
            }
            return rv;
        } catch (Exception ex) {
            throw ex;
        }
    }

    public static ArrayList<DTOHotel> toDTO(List<RoomVo> lrooms) throws Exception{

        ArrayList<DTOHotel> dtolist = new ArrayList<DTOHotel>();

        for (int i = 0; i < lrooms.size(); i++) {
            DTOHotel dto = new DTOHotel();
            String tipo=getTypeName(lrooms.get(i).getIdTypeRoom());
            double costo=getRoomCost(lrooms.get(i).getIdTypeRoom());
            dto.setall("Hotel Jojojo", lrooms.get(i).getId(), lrooms.get(i).getNumber(), lrooms.get(i).getCapacity(), tipo, lrooms.get(i).getDescription(), costo);
            dtolist.add(dto);
        }
        return dtolist;
    }

    public static String getStringReservaHotel(String idReserva) throws Exception{
        int idReservation = Integer.parseInt(idReserva);
        String res = "";
        String numHab = "";
        String capacidad = "";
        String tipo = "";
        String descripcion = "";
        String fecha1 = "";
        String fecha2 = "";
        try{
            ReservationVo resvo = getReservationVo(idReservation);
            RoomVo rvo = getRoomVo(resvo.getIdRoom().getId());
            numHab = ""+rvo.getNumber();
            capacidad = ""+rvo.getCapacity();
            tipo = "";
            descripcion = rvo.getDescription();
            fecha1 = ""+resvo.getReservationDate().getYear()+"-"+resvo.getReservationDate().getMonth()+"-"+resvo.getReservationDate().getDay();
            fecha2 = ""+resvo.getExpirationDate().getYear()+"-"+resvo.getExpirationDate().getMonth()+"-"+resvo.getExpirationDate().getDay();
        }catch(Exception ex){
            throw ex;
        }
        res = "<br>Número Habitación: "+numHab+"<br>Capacidad: "+capacidad+"<br>Tipo: "+tipo+"<br>Descripción: "+descripcion+"<br>Fecha de Inicio: "+fecha1+"<br>Fecha de Finalización: "+fecha2;
        return res;
    }

    public static webservices.ReservationVo getReservationVo(int idReserva) throws Exception{
        try { // Call Web Service Operation
            webservices.WebServiceReservaService service = new webservices.WebServiceReservaService();
            webservices.WebServiceReserva port = service.getWebServiceReservaPort();
            // TODO initialize WS operation arguments here
            // TODO process result here
            webservices.ReservationVo result = port.consultarReserva(idReserva);
            System.out.println("Result = "+result);
            return result;
        } catch (Exception ex) {
            throw ex;
            // TODO handle custom exceptions here
        }
    }

    public static boolean cancelarReserva(int idReserva) throws Exception{
        try { // Call Web Service Operation
            webservices.WebServiceReservaService service = new webservices.WebServiceReservaService();
            webservices.WebServiceReserva port = service.getWebServiceReservaPort();
            // TODO initialize WS operation arguments here
            webservices.ReservationVo reservavo = getReservationVo(idReserva);
            // TODO process result here
            boolean result = port.cancelarReserva(reservavo);
            System.out.println("Result = "+result);
            return result;
        } catch (Exception ex) {
            throw ex;
            // TODO handle custom exceptions here
        }
    }

    public static boolean loginUsuario(String usuario, String contrasena) throws Exception{

        try { // Call Web Service Operation
            webservices.WebServiceUsuarioService service = new webservices.WebServiceUsuarioService();
            webservices.WebServiceUsuario port = service.getWebServiceUsuarioPort();
            // TODO initialize WS operation arguments here
            //java.lang.String usuario = "";
            //java.lang.String contrasena = "";
            // TODO process result here
            java.lang.Boolean result = port.login(usuario, contrasena);
            System.out.println("Result = "+result);
            return result;
        } catch (Exception ex) {
            throw ex;
            // TODO handle custom exceptions here
        }
    }

    public static webservices.FinaluserVo consultarUsuario(String identification) throws Exception{

        try { // Call Web Service Operation
            webservices.WebServiceUsuarioService service = new webservices.WebServiceUsuarioService();
            webservices.WebServiceUsuario port = service.getWebServiceUsuarioPort();
            // TODO initialize WS operation arguments here
            //java.lang.String identification = "";
            // TODO process result here
            webservices.FinaluserVo result = port.consultarUsuario(identification);
            System.out.println("Result = "+result);
            return result;
        } catch (Exception ex) {
            throw ex;
            // TODO handle custom exceptions here
        }
    }

    public static boolean crearUsuario(String identificacion, String username, String pass, String name, String lastname, String email, String creditcard, String tel, int type) throws Exception{

        try { // Call Web Service Operation
            webservices.WebServiceUsuarioService service = new webservices.WebServiceUsuarioService();
            webservices.WebServiceUsuario port = service.getWebServiceUsuarioPort();
            // TODO initialize WS operation arguments here
            webservices.FinaluserVo uservo = new webservices.FinaluserVo();
            uservo.setIdentification(identificacion);
            uservo.setUserName(username);
            uservo.setPasswordMD5(pass);
            uservo.setFirstName(name);
            uservo.setLastName(lastname);
            uservo.setEmail(email);
            uservo.setCreditCard(creditcard);
            uservo.setPhoneNumber(tel);
            uservo.setUserType(type);
            // TODO process result here
            boolean result = port.saveUser(uservo);
            System.out.println("Result = "+result);
            return result;
        } catch (Exception ex) {
            throw ex;
            // TODO handle custom exceptions here
        }
    }
}